package com.link2room.aeriabots.service.impl;

import com.link2room.aeriabots.constant.CallBackType;
import com.link2room.aeriabots.service.IWXQrcodeService;
import com.link2room.common.dao.ICommonDao;
import com.link2room.aeriabots.entity.WxQrcodeScanDetailEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LN on 2017/10/30
 */
@Component("wxQrcodeService")
public class WXQrcodeServiceImpl implements IWXQrcodeService {
    @Resource
    private ICommonDao commonDao;
    @Override
    @Transactional
    /**
     * 扫码次数统计
     */
    public void atomicIncrement(String lhotelGroupCode, String qrType, String qrKey, Integer scanCount, Integer subscriberCount) throws Exception {

        List queryParameters = new ArrayList();
        queryParameters.add(lhotelGroupCode);
        queryParameters.add(qrType);
        queryParameters.add(qrKey);
        queryParameters.add(scanCount);
        queryParameters.add(subscriberCount);
        queryParameters.add(scanCount);
        queryParameters.add(subscriberCount);
        commonDao.excuteByNative(atomicIncrementSQL(),queryParameters);
//        commonDao.insertByNative(atomicIncrementSQL());
    }

    @Override
    @Transactional
    public void scanCount(String lhotelGroupCode, String qrType, String qrKey, String scanType, String openid,String msgId) throws Exception {
        WxQrcodeScanDetailEntity saveEntity = new WxQrcodeScanDetailEntity();
        saveEntity.setLhotelGroupCode(lhotelGroupCode);
        saveEntity.setQrType(qrType);
        saveEntity.setQrKey(qrKey);
        saveEntity.setScanType(scanType);
        saveEntity.setMsgId(msgId);
        saveEntity.setOpenid(openid);
        commonDao.save(saveEntity);
        int subscriberCount = 0;
        if(CallBackType.EVENT_TYPE.SUBSCRIBE.equals(scanType)){
            subscriberCount = 1;
        }
        this.atomicIncrement(lhotelGroupCode,qrType,qrKey,1,subscriberCount);
    }



    private String atomicIncrementSQL(){
        return "\n"+
                "INSERT INTO `wx_qrcode_scan_count` (\n" +
                "  `lhotel_group_code`,\n" +
                "  `qr_type`,\n" +
                "  `qr_key`,\n" +
                "  `scan_count`,\n" +
                "  `subscriber_count`,\n" +
                "  `day`,\n" +
                "  `create_datetime`,\n" +
                "  `modify_datetime`\n" +
                ") \n" +
                "VALUES\n" +
                "  (\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    ?,\n" +
                "    CURRENT_DATE,\n" +
                "    CURRENT_DATE,\n" +
                "    CURRENT_DATE\n" +
                "  ) \n" +
                "  ON DUPLICATE KEY \n" +
                "  UPDATE \n" +
                "    scan_count = scan_count + ?,\n" +
                "    subscriber_count = subscriber_count + ?,\n" +
                "    modify_datetime = CURRENT_DATE ;\n" +
                "\n";
    }
}
